package inno.specificSelection;

import inno.treeRepro.Chromosome;

import java.util.ArrayList;
import java.util.Random;

public class RollateSelection implements Selection{

	@Override
	public ArrayList<Chromosome> doSelection(ArrayList<Chromosome> population) {

		ArrayList<Chromosome> chromoAns = new ArrayList<Chromosome>();
		
		int sum = 0;
		for (int i=0;i<population.size();i++){
			sum+=population.get(i).getMyFitness();
		}
		
		Random rand = new Random();
		// chose two chromos randomicly
		int randSum = rand.nextInt(sum);
		int i = 0;
	  
		while (randSum>0){
			randSum-=population.get(i).getMyFitness();
			++i;
		}
		if (population.size()==i){
			--i;
		}
		chromoAns.add(new Chromosome(population.get(i)));
		
		randSum = rand.nextInt(sum);
		i = 0;
	  
		while (randSum>0){
			randSum-=population.get(i).getMyFitness();
			++i;
		}
		if (population.size()==i){
			--i;
		}
		chromoAns.add(new Chromosome(population.get(i)));
		
		return chromoAns;
	}

}
